package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 减少购物车商品数量
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // select * from shopping_cart where dish_id = ? and user_id=?
        shoppingCart=shoppingCartMapper.selectList(shoppingCart).get(0);
        if(shoppingCart != null){
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
            if(shoppingCart.getNumber() == 0){
                // 若数量为0，则删除购物车中的菜品
                shoppingCartMapper.delete(shoppingCart);
            }
        }
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        // 配置属性拷贝dishId，setmealId，dishFlavor
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //只能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //1. 判断当前商品【套餐/菜品】是否在购物车中,select * from shopping_cart where dish_id = ? or setmeal_id =? and user_id=?
        List<ShoppingCart> carts = shoppingCartMapper.selectList(shoppingCart);// carts只可能是一条数据

        if(carts!=null && carts.size()==1){
            // 如果carts不为空，则说明购物车中有当前【套餐/菜品】,number +1
            shoppingCart = carts.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else{
            // 购物车中没有数据，设置 number=1

            //判断当前添加到购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){// 若是dish
                // 若是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            }else {
                Long setmealId = shoppingCartDTO.getSetmealId();// 若为套餐setmealId!=null
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            // 插入购物车
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 购物车列表
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        // 获取当前微信用户id
        Long id = BaseContext.getCurrentId();

        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(id)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.selectList(shoppingCart);
        return list;
    }

    /**
     * 根据user_id
     * 清空购物车
     */
    public void cleanAll() {
        // 获取当前微信用户id
        Long id = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(id);
    }
}
